package com.greatmay.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;

/**
 * This class represents the basic "user" object in AppFuse that allows for authentication
 * and user management.  It implements Acegi Security's UserDetails interface.
 *
 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
 *         Updated by Dan Kibler (dan@getrolling.com)
 *         Extended to implement Acegi UserDetails interface
 *         by David Carter david@carter.net
 */
@JsonIgnoreProperties(value={"categories"})
@Entity
@Table(name = "book")
@Indexed
@XmlRootElement
public class Book extends BaseObject {
    private static final long serialVersionUID = -7869107480253849972L;
    private Long id;
    private String category;                    // required
    private String descriptionPic;                    // required
    private String descriptioin;
    private String fileName;                    // relative path to file system and url
    private Long length;                       // required; unique
    private String name;
    private String origination;
    private Date presentDate;
    private String presenter;
    private String previewPic;
    private Double price;
    private Double priceInSales;
    private Long size;

    private ProductStatus status;


    /**
     * Default constructor - creates a new instance with no values set.
     */
    public Book() {
    }


    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @DocumentId
    public Long getId() {
        return id;
    }


    @Column
    @Field
    public String getCategory() {
        return category;
    }


    @Transient @XmlTransient
    public List<String> getCategories() {
        if (category == null || category.isEmpty()) {
            return new ArrayList<String>(0);
        }
        String[] categorisArray = category.split(",");
        List<String> categories = new ArrayList<String>(categorisArray.length);
        Collections.addAll(categories, categorisArray);
        Collections.sort(categories);
        return categories;
    }


    public String getDescriptionPic() {
        return descriptionPic;
    }


    @Column
    @Field
    public String getDescriptioin() {
        return descriptioin;
    }


    @Column(nullable = false, length=511)
    public String getFileName() {
        return fileName;
    }


    public Long getLength() {
        return length;
    }


    @Column(nullable = false, length = 100, unique = true)
    @Field
    public String getName() {
        return name;
    }


    @Column
    @Field
    public String getOrigination() {
        return origination;
    }


    public Date getPresentDate() {
        return presentDate;
    }


    @Column
    @Field
    public String getPresenter() {
        return presenter;
    }


    public String getPreviewPic() {
        return previewPic;
    }


    public Double getPrice() {
        return price;
    }


    public Double getPriceInSales() {
        return priceInSales;
    }


    public Long getSize() {
        return size;
    }


    @Column(nullable=false)
    @Enumerated(EnumType.ORDINAL)
    public ProductStatus getStatus() {
        return status;
    }


    public void setId(Long id) {
        this.id = id;
    }


    public void setCategory(String category) {
        this.category = category;
    }

    public void setCategories(List<String> categories) {
        if (categories != null) {
            StringBuilder sb = new StringBuilder();
            for (String s : categories) {
                sb.append(Category.SPERATER).append(s);
            }
            if (sb.length() >= 1) {
                sb.deleteCharAt(0);
            }
            this.category = sb.toString();
        }
    }

    public void setCategories(String[] categories) {
        if (categories != null) {
            StringBuilder sb = new StringBuilder();
            for (String s : categories) {
                sb.append(Category.SPERATER).append(s);
            }
            if (sb.length() >= 1) {
                sb.deleteCharAt(0);
            }
            this.category = sb.toString();
        }
    }

    public void setDescriptionPic(String descriptionPic) {
        this.descriptionPic = descriptionPic;
    }


    public void setDescriptioin(String descriptioin) {
        this.descriptioin = descriptioin;
    }


    public void setFileName(String fileName) {
        this.fileName = fileName;
    }


    public void setLength(Long length) {
        this.length = length;
    }


    public void setName(String name) {
        this.name = name;
    }


    public void setOrigination(String origination) {
        this.origination = origination;
    }


    public void setPresentDate(Date presentDate) {
        this.presentDate = presentDate;
    }


    public void setPresenter(String presenter) {
        this.presenter = presenter;
    }


    public void setPreviewPic(String previewPic) {
        this.previewPic = previewPic;
    }


    public void setPrice(Double price) {
        this.price = price;
    }


    public void setPriceInSales(Double priceInSales) {
        this.priceInSales = priceInSales;
    }


    public void setSize(Long size) {
        this.size = size;
    }


    public void setStatus(ProductStatus status) {
        this.status = status;
    }

}
